package com.atguigu.demo.excel;

import com.alibaba.excel.EasyExcel;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class ExcelDemo {

    /**
     * 生成excel表格文件测试
     * */
    @Test
    public void excelWrite(){
        String fileName="D:\\test.xlsx";

        //fileName  将要生成的表格文件路径和文件名
        //DemoData为表格 实体数据类
        //doWrite参数为表格 实体数据类的list
        EasyExcel.write(fileName,DemoData.class).sheet("测试表1").doWrite(getDemoDataList());
    }

    /**
     * 读取已有的excel表格文件
     * */
    @Test
    public void excelRead(){
        String filename = "D:\\test.xlsx";
        EasyExcel.read(filename,DemoData.class,new ExcelListener()).sheet().doRead();
    }

    private List<DemoData> getDemoDataList(){
        List<DemoData> list = new ArrayList<DemoData>();
        for (int i=0;i<20;i++){
            DemoData data = new DemoData();
            data.setSn("lj"+i);
            data.setSno(i);
            list.add(data);
        }
        return list;
    }

}
